<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>



    <script>
        var num = 1;
        var myObject = {
            num: 2,
            add: function() {
                this.num = 3;
                (function() {
                    console.log(this.num); //1
                    this.num = 4;
                })();
                console.log(this.num); //3
            },
            sub: function() {
                console.log(this.num) //4
            }
        }
        myObject.add();
        console.log(myObject.num); //3
        console.log(num); //4
        var sub = myObject.sub;
        sub();
    </script>

</body>

</html>